@extends('admin.public.layout')
@section('title', '修改角色')

@section('css')

@endsection

@section('content')
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header"><h4>修改角色</h4></div>
                <div class="card-body">
                    <form action="{{url('admin/role/edit')}}" method="post">
                        <input type="hidden" id="id" name="id" value="<?php echo !empty($item['id']) ? $item['id'] : ''?>"/>
                        <div class="form-group">
                            <label for="example-text-input"><span style="color:red;">*</span>&nbsp;角色名称</label>
                            <input class="form-control" type="text" name="name" placeholder="请输入角色名称" value="<?php echo !empty($item['role_name']) ? $item['role_name'] : ''?>">
                        </div>
                        <div class="form-group">
                            <label for="example-text-input">角色描述</label>
                            <textarea class="form-control" id="remark" rows="5" name="remark" placeholder="请输入角色描述"><?php echo !empty($item['remark']) ? $item['remark'] : ''?></textarea>
                            <small class="help-block">角色描述，角色所属操作权限</small>
                        </div>
                        <div class="table-responsive">
                            <label for="example-text-input">角色权限</label>
                            <table class="table table-striped">
                                <thead>
                                <tr>
                                    <th>
                                        <label class="lyear-checkbox checkbox-primary">
                                            <input name="checkbox" type="checkbox" id="check-all">
                                            <span> 全选</span>
                                        </label>
                                    </th>
                                </tr>
                                </thead>
                                <tbody>
                                @if(count($menu_list) > 0)
                                    @foreach($menu_list as $k=>$v)
                                    <tr>
                                        <td>
                                            <label class="lyear-checkbox checkbox-primary">
                                                <input name="rules[]" type="checkbox" class="checkbox-parent" dataid="id-{{$v['id']}}" value="{{$v['id']}}" <?php if(isset($role_menu) && in_array($v['id'],$role_menu)){?>checked<?php }?>>
                                                <span> {{$v['menu_name']}}</span>
                                            </label>
                                        </td>
                                    </tr>
                                    @if(count($v['child']) > 0)
                                        @foreach($v['child'] as $ck=>$cv)
                                        <tr>
                                            <td class="p-l-30">
                                                <label class="lyear-checkbox checkbox-primary">
                                                    <input name="rules[]" type="checkbox" class="checkbox-parent checkbox-child" dataid="id-{{$v['id']}}-{{$cv['id']}}" value="{{$cv['id']}}" <?php if(isset($role_menu) && in_array($cv['id'],$role_menu)){?>checked<?php }?>>
                                                    <span> {{$cv['menu_name']}}</span>
                                                </label>
                                            </td>
                                        </tr>
                                        @if(count($cv['child']) > 0)
                                        <tr>
                                            <td class="p-l-60">
                                                @foreach($cv['child'] as $tk=>$tv)
                                                <label class="lyear-checkbox checkbox-primary checkbox-inline">
                                                    <input name="rules[]" type="checkbox" class="checkbox-child" dataid="id-{{$v['id']}}-{{$cv['id']}}-{{$tv['id']}}" value="{{$tv['id']}}" <?php if(isset($role_menu) && in_array($tv['id'],$role_menu)){?>checked<?php }?>>
                                                    <span> {{$tv['menu_name']}}</span>
                                                </label>
                                                @endforeach
                                            </td>
                                        </tr>
                                        @endif
                                        @endforeach
                                    @endif
                                    @endforeach
                                @endif
                                </tbody>
                            </table>
                        </div>
                        <div class="form-group">
                            <button class="btn btn-primary submitForm" type="button">修改</button>
                            <button class="btn btn-secondary" type="button" onclick="window.history.back(-1);">返回</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
    <script type="text/javascript" src="{{asset('static/js/chosen.jquery.min.js')}}"></script>
    <script type="text/javascript">
        $(function(){
            //动态选择框，上下级选中状态变化
            $('input.checkbox-parent').on('change', function(){
                var dataid = $(this).attr("dataid");
                $('input[dataid^=' + dataid + '-]').prop('checked', $(this).is(':checked'));
            });
            $('input.checkbox-child').on('change', function(){
                var dataid = $(this).attr("dataid");
                dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                var parent = $('input[dataid=' + dataid + ']');
                if($(this).is(':checked')){
                    parent.prop('checked', true);
                    //循环到顶级
                    while(dataid.lastIndexOf("-") != 2){
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        parent.prop('checked', true);
                    }
                }else{
                    //父级
                    if($('input[dataid^=' + dataid + '-]:checked').length == 0){
                        parent.prop('checked', false);
                        //循环到顶级
                        while(dataid.lastIndexOf("-") != 2){
                            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                            parent = $('input[dataid=' + dataid + ']');
                            if($('input[dataid^=' + dataid + '-]:checked').length == 0){
                                parent.prop('checked', false);
                            }
                        }
                    }
                }
            });
        });
    </script>
@endsection